package com.zzx.controller;

import com.zzx.entity.ApiResult;
import com.zzx.entity.TCompany;
import com.zzx.entity.TUser;
import com.zzx.service.TCompanyService;
import com.zzx.utils.QRCodeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "公司 模块管理")
@RestController
@RequestMapping("/TCompany")
public class TCompanyController {

    @Resource
    private TCompanyService tCompanyService;

    @Resource
    private QRCodeGenerator qrCodeGenerator;

    @ApiOperation(value = "新增、编辑")
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public ApiResult add(@RequestBody TCompany param) {
        tCompanyService.add(param);
        return ApiResult.ok();
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value="/deleteById",method = RequestMethod.POST)
    public ApiResult deleteById(@RequestBody TCompany param) {
        tCompanyService.deleteById(param);
        return ApiResult.ok();
    }

    @ApiOperation(value = "列表查询")
    @RequestMapping(value="/selectBy",method = RequestMethod.POST)
    public ApiResult selectBy(@RequestBody TCompany param) {

        return ApiResult.ok(tCompanyService.selectBy(param));
    }

    @ApiOperation(value = "生成二维码")
    @RequestMapping(value="/generateQR/{companyId}",method = RequestMethod.GET)
    public ApiResult generateQR(@PathVariable Long companyId) {
        byte[] bytes = qrCodeGenerator.generateAndSaveQRCode(companyId.toString());
        TCompany byId = tCompanyService.getById(companyId);
        byId.setCompanyQrCode(bytes);
        tCompanyService.saveOrUpdate(byId);
        return ApiResult.ok();
    }

    /**
     * 接口说明：根据 companyId 获取公司二维码图片（二进制流）
     */
    @ApiOperation(value = "获取公司二维码图片")
    @GetMapping("/qr-code/{companyId}")
    public ResponseEntity<ByteArrayResource> getCompanyQrCode(@PathVariable Long companyId) {
        TCompany company = tCompanyService.getById(companyId);

        if (company == null) {
            return ResponseEntity.notFound().build();
        }

        byte[] qrCodeBytes = company.getCompanyQrCode();
        if (qrCodeBytes == null || qrCodeBytes.length == 0) {
            return ResponseEntity.noContent().build();
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        headers.setContentDisposition(ContentDisposition.builder("inline")
                .filename("company-qr.png")
                .build());

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(qrCodeBytes.length)
                .body(new ByteArrayResource(qrCodeBytes));
    }


    @ApiOperation(value = "sayHello")
    @RequestMapping(value="/sayHello",method = RequestMethod.POST)
    public ApiResult sayHello() {
        return ApiResult.ok("你好!");
    }

    @ApiOperation(value = "根据ID查询")
    @RequestMapping(value="/findById/{companyId}",method = RequestMethod.GET)
    public ApiResult findById(@PathVariable String companyId ) {
        TCompany res = tCompanyService.findById(companyId);
        return ApiResult.ok(res);
    }
}
